package daily;

/**
 * @author MtRider
 * @version 1.0
 * @description 验证回文串
 * 输入: "A man, a plan, a canal: Panama"
 * 输出: true 解释："amanaplanacanalpanama" 是回文串
 * @GiteeAndGithub MtRider
 * @date 2022/4/24 20:47
 */
public class demo16 {
    public static void main(String[] args) {
        System.out.println(isPalindrome("race a car"));
    }

    //isLetterOrDigit判断是否是字母或数字
    public static boolean isPalindrome(String s) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char currentASCII = s.charAt(i);
            if (currentASCII >= 48 && currentASCII <= 57
                    || currentASCII >= 65 && currentASCII <= 90
                    || currentASCII >= 97 && currentASCII <= 122) {
               buffer.append(currentASCII);
            }
        }
        s=buffer.toString().toLowerCase();
        for (int i = 0; i < s.length()/2; i++) {
            if (s.charAt(i)!=s.charAt(s.length()-1-i)){
                return false;
            }
        }
        return true;
    }
}
